package com.fqgj.sentry.biz.rm;

import com.fqgj.report.statistics.entity.BiAuditStatisticEntity;
import com.fqgj.sentry.biz.rm.vo.CategoryVo;
import com.fqgj.sentry.biz.rm.vo.OrderRiskTableVo;
import com.fqgj.sentry.biz.rm.vo.OverdueRiskCharVo;
import com.fqgj.sentry.biz.rm.vo.OverdueRiskTableVo;
import com.fqgj.sentry.common.utils.Arith;
import com.fqgj.sentry.controller.rm.request.OverdueQueryTermsRequest;
import com.fqgj.sentry.statistics.service.OverdueStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-10-13 下午4:09
 */


@Component
public class OverdueStatisticsBusiness {

    @Autowired
    private OverdueStatisticsService overdueStatisticsService;

    public OverdueRiskTableVo getOverdueStatisticTable(OverdueQueryTermsRequest overdueQueryTermsRequest) {

        List<BiAuditStatisticEntity> entityList = overdueStatisticsService.selectRiskAuditStatistic(
                overdueQueryTermsRequest.getStartDate(), overdueQueryTermsRequest.getEndDate(), overdueQueryTermsRequest.getEventId(),
                overdueQueryTermsRequest.getExecutorType(), overdueQueryTermsRequest.getVersion(),
                overdueQueryTermsRequest.getSource(), overdueQueryTermsRequest.getBorrowType());

        OverdueRiskTableVo overdueRiskTableVo = new OverdueRiskTableVo();
        List<OrderRiskTableVo> orderRiskTableVos = new ArrayList<>();

        OrderRiskTableVo totalOrderTableVo = new OrderRiskTableVo()
                .setApplyNum(0L)
                .setDate("全部")
                .setPassNum(0L)
                .setPassRate(0.0)
                .setRejectNum(0L)
                .setRejectRate(0.0)
                .setReviewNum(0L)
                .setReviewRate(0.0)
                .setLoanNum(0L)
                .setLoanPrincipal(0.0)
                .setLoanRate(0.0)
                .setLoanAmount(0.0)
                .setOnOverdueRate(0.0)
                .setBackRate(0.0)
                .setOneDayOverdueRate(0.0)
                .setFifteenOverdueBackRate(0.0)
                .setFifteenDayOverdueRate(0.0);


        //总的额在逾个数
        Long totalOnOverdueCnt = 0L;
        //总的首逾应还数
        Long totalOneDayBackCnt = 0L;
        //总的首逾个数
        Long totalOneDayOverdueCnt = 0L;
        //总的d15应还数
        Long totalFifteenBackCnt = 0L;
        //总的d15逾期数
        Long totalFifteenOverdueCnt = 0L;

        for (BiAuditStatisticEntity entity : entityList) {
            OrderRiskTableVo orderRiskTableVo = new OrderRiskTableVo();
            orderRiskTableVo.setDate(entity.getApplyDate())
                    .setApplyNum(entity.getApplyCnt())
                    .setRejectNum(entity.getRejectCnt())
                    .setPassNum(entity.getPassCnt())
                    .setReviewNum(entity.getManualCnt())
                    .setRejectRate(Arith.divide(entity.getRejectCnt() * 100, entity.getApplyCnt()))
                    .setReviewRate(Arith.divide(entity.getManualCnt() * 100, entity.getApplyCnt()))
                    .setPassRate(Arith.divide(entity.getPassCnt() * 100, entity.getApplyCnt()));

            totalOrderTableVo.setApplyNum(entity.getApplyCnt() + totalOrderTableVo.getApplyNum())
                    .setPassNum(entity.getPassCnt() + totalOrderTableVo.getPassNum())
                    .setRejectNum(entity.getRejectCnt() + totalOrderTableVo.getRejectNum())
                    .setReviewNum(entity.getManualCnt() + totalOrderTableVo.getReviewNum());

            orderRiskTableVo.setDate(entity.getApplyDate())
                    .setLoanNum(entity.getPaidCnt())
                    .setLoanPrincipal(entity.getTotalApplyCapital())
                    .setLoanRate(Arith.divide(entity.getPaidCnt() * 100, entity.getApplyCnt()))
                    .setLoanAmount(entity.getTotalPaidCapital());
            if (!entity.getOneDayBackCnt().equals(0L)) {
                orderRiskTableVo.setOnOverdueRate(Arith.divide(entity.getOnOverdueCnt() * 100, entity.getOneDayBackCnt()));
                orderRiskTableVo.setOneDayOverdueRate(Arith.divide(entity.getOneDayCnt() * 100, entity.getOneDayBackCnt()));
            } else {
                orderRiskTableVo.setOnOverdueRate(0.0);
                orderRiskTableVo.setOneDayOverdueRate(0.0);
            }
            if (!entity.getPaidCnt().equals(0L)) {
                orderRiskTableVo.setBackRate(Arith.divide(entity.getOneDayBackCnt() * 100, entity.getPaidCnt()));
                orderRiskTableVo.setFifteenOverdueBackRate(Arith.divide(entity.getFifteenOverdueBackCnt() * 100, entity.getPaidCnt()));
            } else {
                orderRiskTableVo.setBackRate(0.0);
                orderRiskTableVo.setFifteenOverdueBackRate(0.0);
            }
            if (!entity.getFifteenOverdueBackCnt().equals(0L)) {
                orderRiskTableVo.setFifteenDayOverdueRate(Arith.divide(entity.getFifteenOverdueCnt() * 100, entity.getFifteenOverdueBackCnt()));
            } else {
                orderRiskTableVo.setFifteenDayOverdueRate(0.0);
            }

            totalOnOverdueCnt += entity.getOnOverdueCnt();
            totalOneDayBackCnt += entity.getOneDayBackCnt();
            totalOneDayOverdueCnt += entity.getOneDayCnt();
            totalFifteenBackCnt += entity.getFifteenOverdueBackCnt();
            totalFifteenOverdueCnt += entity.getFifteenOverdueCnt();

            totalOrderTableVo.setLoanNum(totalOrderTableVo.getLoanNum() + entity.getPaidCnt())
                    .setLoanPrincipal(Arith.add(totalOrderTableVo.getLoanPrincipal(), entity.getTotalApplyCapital()))
                    .setLoanAmount(Arith.add(totalOrderTableVo.getLoanAmount(), entity.getTotalPaidCapital()));

            orderRiskTableVos.add(orderRiskTableVo);
        }
        //计算总体的
        if (!totalOrderTableVo.getApplyNum().equals(0L)) {
            totalOrderTableVo.setPassRate(Arith.divide(totalOrderTableVo.getPassNum() * 100, totalOrderTableVo.getApplyNum()))
                    .setRejectRate(Arith.divide(totalOrderTableVo.getRejectNum() * 100, totalOrderTableVo.getApplyNum()))
                    .setReviewRate(Arith.divide(totalOrderTableVo.getReviewNum() * 100, totalOrderTableVo.getApplyNum()));
            //放款率
            totalOrderTableVo.setLoanRate(Arith.divide(totalOrderTableVo.getLoanNum() * 100, totalOrderTableVo.getApplyNum()));
        }

        if (!totalOneDayBackCnt.equals(0L)) {
            totalOrderTableVo.setOnOverdueRate(Arith.divide(totalOnOverdueCnt * 100, totalOneDayBackCnt));
            totalOrderTableVo.setOneDayOverdueRate(Arith.divide(totalOneDayOverdueCnt * 100, totalOneDayBackCnt));
        }

        if (!totalOrderTableVo.getLoanNum().equals(0L)) {
            totalOrderTableVo.setBackRate(Arith.divide(totalOneDayBackCnt * 100, totalOrderTableVo.getLoanNum()));
            totalOrderTableVo.setFifteenOverdueBackRate(Arith.divide(totalFifteenBackCnt * 100, totalOrderTableVo.getLoanNum()));
        }
        if (!totalFifteenBackCnt.equals(0L)) {
            totalOrderTableVo.setFifteenDayOverdueRate(Arith.divide(totalFifteenOverdueCnt * 100, totalFifteenBackCnt));
        }
        orderRiskTableVos.add(totalOrderTableVo);

        overdueRiskTableVo.setOrderRiskTableVoList(orderRiskTableVos);
        return overdueRiskTableVo;
    }


    public OverdueRiskCharVo getOverdueStatisticChart(OverdueQueryTermsRequest overdueQueryTermsRequest) {

        List<BiAuditStatisticEntity> entityList = overdueStatisticsService.selectRiskAuditStatistic(
                overdueQueryTermsRequest.getStartDate(), overdueQueryTermsRequest.getEndDate(), overdueQueryTermsRequest.getEventId(),
                overdueQueryTermsRequest.getExecutorType(), overdueQueryTermsRequest.getVersion(),
                overdueQueryTermsRequest.getSource(), overdueQueryTermsRequest.getBorrowType());

        OverdueRiskCharVo overdueRiskCharVo = new OverdueRiskCharVo();

        List<String> xAxis = new ArrayList<>();

        List<CategoryVo<Long>> columnSeries = new LinkedList<>();

        List<CategoryVo<Double>> lineSeries = new LinkedList<>();

        if (entityList.size() != 0) {
            CategoryVo<Long> passCategory = new CategoryVo<>();
            passCategory.setName("通过数");
            CategoryVo<Long> reviewCategory = new CategoryVo<>();
            reviewCategory.setName("人工数");
            CategoryVo<Long> rejectCategory = new CategoryVo<>();
            rejectCategory.setName("拒绝数");

            CategoryVo<Double> oneDayOverdueRate = new CategoryVo<>();
            oneDayOverdueRate.setName("首逾率(个数%)");

            CategoryVo<Double> passRate = new CategoryVo<>();
            passRate.setName("通过率(%)");


            CategoryVo<Double> fifteenDayOverdueRate = new CategoryVo<>();
            fifteenDayOverdueRate.setName("d15逾期率(个数%)");

            List<Long> passList = new LinkedList<>();
            List<Long> rejectList = new LinkedList<>();
            List<Long> reviewList = new LinkedList<>();

            List<Double> oneDayOverdueList = new LinkedList<>();
            List<Double> fifteenDayList = new LinkedList<>();
            List<Double> passRateList = new LinkedList<>();


            for (BiAuditStatisticEntity entity : entityList) {
                xAxis.add(entity.getApplyDate());
                passList.add(entity.getPassCnt());
                rejectList.add(entity.getRejectCnt());
                reviewList.add(entity.getManualCnt());
                //首逾率
                if (entity.getOneDayBackCnt() != 0) {
                    oneDayOverdueList.add(Arith.divide(entity.getOneDayCnt() * 100, entity.getOneDayBackCnt()));
                } else {
                    oneDayOverdueList.add(0.0);
                }
                if (entity.getFifteenOverdueBackCnt() != 0) {
                    fifteenDayList.add(Arith.divide(entity.getFifteenOverdueCnt() * 100, entity.getFifteenOverdueBackCnt()));
                } else {
                    fifteenDayList.add(0.0);
                }

                passRateList.add(Arith.divide(entity.getPassCnt() * 100, entity.getApplyCnt()));
            }
            reviewCategory.setData(reviewList);
            rejectCategory.setData(rejectList);
            passCategory.setData(passList);


            passRate.setData(passRateList);
            oneDayOverdueRate.setData(oneDayOverdueList);
            fifteenDayOverdueRate.setData(fifteenDayList);

            columnSeries.add(passCategory);
            columnSeries.add(reviewCategory);
            columnSeries.add(rejectCategory);

            lineSeries.add(oneDayOverdueRate);
            lineSeries.add(fifteenDayOverdueRate);
            lineSeries.add(passRate);
        }

        overdueRiskCharVo.setxAxis(xAxis);
        overdueRiskCharVo.setColumnSeries(columnSeries);
        overdueRiskCharVo.setLineSeries(lineSeries);

        return overdueRiskCharVo;
    }
}
